package com.infinit.easyreader.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.infinit.easyreader.entity.ContentType;
import com.infinit.easyreader.entity.RankTime;
import com.infinit.easyreader.entity.RankType;
import com.infinit.easyreader.utils.Consts;

import java.util.ArrayList;
import java.util.List;

/**
 * 类：com.infinit.easyreader.dao.RankDao V[1.0.0]
 * 作用：获取Rank相关的数据
 * Created by rzy on 2016/6/26.
 */
public class RankDao extends SQLiteOpenHelper implements Consts {
    private String rankType = "rankType";
    private String rankTime = "rankTime";
    private String contentType = "contentType";
    private SQLiteDatabase database;


    public RankDao(Context context) {
        super(context, dataBaseName, null, 1);
        database = getWritableDatabase();
    }


    /**
     * 获取所有的rank类信息
     *
     * @return rank列表
     */
    public List<RankType> getAllRankType() {
        String colomns[] = {
                "ranktype_id", //0
                "rankName" //1
        };
        Cursor cursor = database.query(rankType, colomns, null, null, null, null, null);
        if (cursor.getCount() == 0) {
            return null;
        }
        List<RankType> rankTypes = new ArrayList<>();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            RankType rankType = new RankType();
            rankType.setRankTypeID(cursor.getInt(0));
            rankType.setRankName(cursor.getString(1));
            rankTypes.add(rankType);
            cursor.moveToNext();
        }
        cursor.close();
        return rankTypes;
    }

    /**
     * 作用：获取所有的时间排行信息
     */

    public List<RankTime> getAllRankTime() {
        String colomns[] = {
                "rankTimeId", //0
                "rankTimeName" //1
        };
        Cursor cursor = database.query(rankTime, colomns, null, null, null, null, null);
        if (cursor.getCount() == 0) {
            return null;
        }

        cursor.moveToFirst();
        List<RankTime> rankTimes = new ArrayList<>();
        while (!cursor.isAfterLast()) {

            RankTime rankTime = new RankTime();
            rankTime.setRankTimeID(cursor.getInt(0));
            rankTime.setRankTimeName(cursor.getString(1));
            rankTimes.add(rankTime);
            cursor.moveToNext();
        }
        cursor.close();
        return rankTimes;
    }


    /**
     * 作用：获取内容列表
     */


    public List<ContentType> getContentTypes() {

        String colomns[] = {
                "contentTypeId",
                "contentType"
        };
        Cursor cursor = database.query(contentType, colomns, null, null, null, null, null);
        if (cursor.getCount() == 0) {
            cursor.close();
            return null;
        }
        List<ContentType> types = new ArrayList<>();
        if (!cursor.isAfterLast()) {
            ContentType type = new ContentType();
            type.setContentTypeID(cursor.getInt(0));
            type.setContentType(cursor.getString(1));
            types.add(type);
            cursor.moveToNext();
        }
        cursor.close();
        return types;
    }


    /***
     * 插入Rank信息
     *
     * @param mList Rank数据
     */
    public void insertRankInfo(List mList) {
        Object object = mList.get(0);
        if (object instanceof RankType) {
            for (Object o : mList) {
                RankType type = (RankType) o;
                ContentValues values = new ContentValues();
                values.put("ranktype_id", type.getRankTypeID());
                values.put("rankName", type.getRankName());
                database.insertWithOnConflict(rankType, null, values, SQLiteDatabase.CONFLICT_ABORT);
            }
        } else if (object instanceof RankTime) {
            for (Object o : mList) {
                RankTime time = (RankTime) o;
                ContentValues values = new ContentValues();
                values.put("rankTimeId", time.getRankTimeID());
                values.put("rankTimeName", time.getRankTimeName());
                database.insertWithOnConflict(rankTime, null, values, SQLiteDatabase.CONFLICT_IGNORE);
            }


        } else {
            for (Object o : mList) {
                ContentType type = (ContentType) o;
                ContentValues values = new ContentValues();
                values.put("contentTypeId", type.getContentTypeID());
                values.put("contentType", type.getContentType());
                database.insertWithOnConflict(contentType, null, values, SQLiteDatabase.CONFLICT_IGNORE);
            }


        }
    }


    /**
     * 查询指定的RankType
     */

    public RankType getRankType(String rankType) {
        String columns[] = {
                "rankName",
                "ranktype_id"
        };
        Cursor cursor = database.query(this.rankType, columns, " rankName = ?", new String[]{rankType}, null, null, null);
        if (cursor.getCount() == 0) {
            cursor.close();
            return null;
        }
        cursor.moveToFirst();
        RankType rType = new RankType();
        rType.setRankName(cursor.getString(0));
        rType.setRankTypeID(cursor.getInt(1));
        cursor.close();
        return rType;
    }

    /**
     * 查询指定的RankTime
     */

    public RankTime getRankTime(String rankTimeName) {
        String columns[] = {"rankTimeId", "rankTimeName"};
        Cursor cursor = database.query(rankTime, columns, "rankTimeName = ?", new String[]{rankTimeName}, null, null, null);
        if (cursor.getCount() == 0) {
            cursor.close();
            return null;
        }
        cursor.moveToFirst();
        RankTime rankTime = new RankTime();
        rankTime.setRankTimeID(cursor.getInt(0));
        rankTime.setRankTimeName(cursor.getString(1));
        cursor.close();
        return rankTime;
    }


    /**
     * 清除数据库中RankType、RankTime、ContentType这三张表中的数据
     */

    public void clearData() {
        String clearContentType = "delete from 'contentType' where '_id'>=0";
        String clearRankTime = "delete from  'rankTime' where '_id'>=0";
        String clearRankType = "delete from  'rankType' where '_id'>=0";
        database.execSQL(clearContentType);
        database.execSQL(clearRankTime);
        database.execSQL(clearRankType);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void destroy() {
        database.close();
    }
}
